<template>
  <el-container class="app-wrapper">
    <!-- 侧边栏 -->
    <el-aside width="210px" class="sidebar-container">
      <!-- Logo -->
      <div class="logo-box">
        <img src="@/assets/logo.png" alt="logo" class="sidebar-logo">
        <span class="sidebar-title">阿财阅读</span>
      </div>

      <!-- 菜单 -->
      <el-menu
          :default-active="activeMenu"
          class="sidebar-menu"
          background-color="#304156"
          text-color="#bfcbd9"
          active-text-color="#409EFF"
          :collapse="isCollapse"
          router
      >
        <el-menu-item index="/dashboard">
          <i class="el-icon-s-home"></i>
          <span slot="title" @click="handleRoute('/dashboard')">管理首页</span>
        </el-menu-item>

        <el-menu-item-group>
          <template slot="title">😀流水模块</template>
          <!-- 财务管理 -->
          <el-submenu index="2">
            <template slot="title">
              <i class="el-icon-document"></i>
              <span>财务管理</span>
            </template>
            <el-menu-item index="/pay">
              <i class="el-icon-edit"></i>
              <span @click="handleRoute('/pay')">充值列表</span>
            </el-menu-item>
            <el-menu-item index="/banner">
              <i class="el-icon-edit"></i>
              <span @click="handleRoute('/banner')">广告列表</span>
            </el-menu-item>
          </el-submenu>

          <!-- 管理模块 -->
          <el-menu-item-group>
            <template slot="title">🆒管理模块</template>

            <el-submenu index="3">
              <template slot="title">
                <i class="el-icon-date"></i>
                <span>用户管理</span>
              </template>
              <el-menu-item index="/user/list">
                <i class="el-icon-edit"></i>
                <span @click="handleRoute('/user/list')">用户列表</span>
              </el-menu-item>
              <el-menu-item index="/user/author">
                <i class="el-icon-search"></i>
                <span @click="handleRoute('/user/author')">作家列表</span>
              </el-menu-item>
            </el-submenu>

            <!-- 小说模块 -->
            <el-menu-item-group>
              <template slot="title">📕小说模块</template>

              <el-submenu index="5">
                <template slot="title">
                  <i class="el-icon-reading"></i>
                  <span>原创小说</span>
                </template>
                <el-menu-item index="/novel/list">
                  <i class="el-icon-edit"></i>
                  <span @click="handleRoute('/novel/list')">小说列表</span>
                </el-menu-item>
                <el-menu-item index="/novel/chapter">
                  <i class="el-icon-search"></i>
                  <span @click="handleRoute('/novel/chapter')">章节管理</span>
                </el-menu-item>
                <el-menu-item index="/novel/comment">
                  <i class="el-icon-search"></i>
                  <span @click="handleRoute('/novel/comment')">评论管理</span>
                </el-menu-item>
              </el-submenu>
            </el-menu-item-group>

            <el-submenu index="6">
              <template slot="title">
                <i class="el-icon-collection"></i>
                <span>分类标签</span>
              </template>
              <el-menu-item index="/category">
                <i class="el-icon-edit"></i>
                <span @click="handleRoute('/category')">分类列表</span>
              </el-menu-item>
              <el-menu-item index="/tag">
                <i class="el-icon-search"></i>
                <span @click="handleRoute('/tag')">标签管理</span>
              </el-menu-item>
            </el-submenu>
          </el-menu-item-group>
        </el-menu-item-group>
      </el-menu>
    </el-aside>

    <!-- 主要内容区 -->
    <el-container class="main-container">
      <el-header height="50px" class="navbar">
        <div class="right-menu">
          <el-dropdown class="avatar-container">
              <span class="el-dropdown-link">
                管理员<i class="el-icon-arrow-down el-icon--right"></i>
              </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item>个人中心</el-dropdown-item>
              <el-dropdown-item divided>退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </el-header>

      <el-main class="main-content">
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'LayoutView',
  data() {
    return {
      activeMenu: '1',
      isCollapse: false
    }
  },
  methods: {
    handleRoute(path) {
      this.$router.push(path).catch(err => {
        if (err.name !== 'NavigationDuplicated') {
          throw err;
        }
        // 可以选择是否需要在控制台输出提示
        // console.log('当前已在该页面');
      });
    }
  }
}
</script>

<style scoped>
.app-wrapper {
  height: 100vh;
  min-height: 100vh;
  position: relative;
  overflow: hidden;
}

.sidebar-container {
  background-color: #304156;
  height: 100vh;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 1001;
  overflow-y: auto;
}

.logo-box {
  height: 50px;
  line-height: 50px;
  background: #2b2f3a;
  padding: 0 15px;
  display: flex;
  align-items: center;
}

.sidebar-logo {
  width: 32px;
  height: 32px;
  margin-right: 12px;
}

.sidebar-title {
  color: #fff;
  font-weight: 600;
  font-size: 14px;
}

.sidebar-menu {
  border: none;
  height: 100%;
  width: 100% !important;
}

.navbar {
  height: 50px;
  overflow: hidden;
  position: fixed;
  right: 0;
  top: 0;
  background: #fff;
  box-shadow: 0 1px 4px rgba(0,21,41,.08);
  padding: 0 20px;
  width: calc(100% - 210px);
  z-index: 1000;
}

.right-menu {
  float: right;
  height: 100%;
  line-height: 50px;
}

.avatar-container {
  cursor: pointer;
}

.main-container {
  margin-left: 210px;
  background-color: #f0f2f5;
  overflow-x: hidden;
}

.main-content {
  margin-top: 50px;
  padding: 20px;
}

.app-container {
  padding: 15px;
}
</style>
